package com.sbck.cs3.fragment;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.sbck.cs3.DeviceSelectActivity;
import com.sbck.cs3.MyClass.MessageType;
import com.sbck.cs3.MyGlobal;
import com.sbck.cs3.MyClass.MyOpenHelper;
import com.sbck.cs3.R;
import com.sbck.cs3.databinding.FragmentDeviceinfoBinding;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class DeviceInfoFragment extends Fragment {

    FragmentDeviceinfoBinding  mBinding;
    MyOpenHelper openHelper;
    Button buttonSave ;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_deviceinfo,container,false);



        mBinding = FragmentDeviceinfoBinding.inflate(inflater,container,false);
        mBinding.setUnitinfo(MyGlobal.unitinfo);
        return mBinding.getRoot();
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //EventBus
        if(!EventBus.getDefault().isRegistered(this))
        {
            EventBus.getDefault().register(this);
        }

        if(MyGlobal.unitinfo.IsNew.get() == false)
        {
            EditText editText = view.findViewById(R.id.Et_DeviceInfo_DTU);
            editText.setEnabled(false);
        }

        //保存按钮
        Button button = view.findViewById(R.id.Btn_DeviceInfo_Save);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openHelper = new MyOpenHelper(getActivity());

                if(MyGlobal.unitinfo.DTU == null) {
                    Toast.makeText(getActivity(), "DTU编号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                try
                {
                    Integer.valueOf(MyGlobal.unitinfo.DTU.get());
                }
                catch (Exception ee)
                {
                    Toast.makeText(getActivity(),"DTU编号必须是数字",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(openHelper.IsExist())
                {
                    MyGlobal.unitinfo.IsNew.set(false);
                }

                 if(Boolean.TRUE.equals(MyGlobal.unitinfo.IsNew.get())) {

                     openHelper.Add();
                     Toast.makeText(getActivity(),"保存成功",Toast.LENGTH_SHORT).show();
                 }
                 else
                 {
                     openHelper.Update();

                     Toast.makeText(getActivity(),"修改成功",Toast.LENGTH_SHORT).show();
                 }

            }
        });

        //删除按钮
        Button buttonDel = view.findViewById(R.id.Btn_DeviceInfo_Delete);
        buttonDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                openHelper = new MyOpenHelper(getActivity());
                openHelper.Delete();
                Toast.makeText(getActivity(),"删除成功",Toast.LENGTH_SHORT).show();

                Intent intent = new Intent();
                intent.setClass(getActivity(), DeviceSelectActivity.class);
                startActivity(intent);
            }
        });


        Button buttonChangeFire = view.findViewById(R.id.Btn_DeviceInfo_ChangeFire);
        buttonChangeFire.setOnClickListener(view1 -> {

                // 创建提醒对话框的建造器
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("请注意!!!"); // 设置对话框的标题文本

                builder.setMessage("只有在焰炉处于全部更换焰条后,方可选择使用更换焰条功能，负责会导致后续焰条状态异常!"); // 设置对话框的内容文本
                // 设置对话框的肯定按钮文本及其点击监听器
                builder.setPositiveButton("更换", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        String msg = "下发更换焰条指令...";
                        MyGlobal.socketUtil.SendCmd_FireReset();
                        ShowToast(msg);
                        MyGlobal.AddLog(msg,Color.DKGRAY);
                    }
                });
                // 设置对话框的否定按钮文本及其点击监听器
                builder.setNegativeButton("取消操作", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                AlertDialog alert = builder.create(); // 根据建造器构建提醒对话框对象
                alert.show(); // 显示提醒对话框

        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        if(EventBus.getDefault().isRegistered(this))
        {
            EventBus.getDefault().unregister(this);
        }
    }

    private void ShowToast(String msg)
    {
        if(Boolean.TRUE.equals(MyGlobal.IsPopToast.get()))
            Toast.makeText(getActivity(),msg , Toast.LENGTH_SHORT).show();
    }
    //事件总线
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageType messageType) {
        if(messageType != MessageType.SendCmd) {
            String msg = "收到: [" + messageType.getName() + "] 命令回复";
            ShowToast(msg);
        }

    }


}
